package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.PmsProduct;
import cn.zcscloud.mall.service.PmsProductService;
import cn.zcscloud.mall.vo.CreateVo;
import cn.zcscloud.mall.vo.PmsProductResult;
import cn.zcscloud.mall.vo.PmsProductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/product")
public class PmsProductController {
    @Autowired
    private PmsProductService pmsProductService;

    /**
     * 分页查询
     * @param pmsProductVo 查询条件
     * @return 分页数据
     */
    @GetMapping("/list")
    public R getList( PmsProductVo pmsProductVo){

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(pmsProductVo.getProductSn() != null,PmsProduct::getProductSn,pmsProductVo.getProductSn())
                .like(pmsProductVo.getKeywords() != null,PmsProduct::getKeywords,pmsProductVo.getKeywords())
                .eq(pmsProductVo.getProductCategoryId() != null,PmsProduct::getProductCategoryId,pmsProductVo.getProductCategoryId())
                .eq(pmsProductVo.getProductSn() != null,PmsProduct::getProductSn,pmsProductVo.getProductSn())
                .eq(pmsProductVo.getPublishStatus() != null,PmsProduct::getPublishStatus,pmsProductVo.getPublishStatus())
                    .eq(pmsProductVo.getBrandId() != null,PmsProduct::getBrandId,pmsProductVo.getBrandId())
                .eq(pmsProductVo.getVerifyStatus() != null,PmsProduct::getVerifyStatus,pmsProductVo.getVerifyStatus())
                .orderByDesc(PmsProduct::getSort);

        log.info("pmsProductVo == {}",pmsProductVo);
        System.out.println("pmsProductVo = " + pmsProductVo);
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(Math.toIntExact(pmsProductVo.getPageNum()));
        pageRequest.setPageSize(Math.toIntExact(pmsProductVo.getPageSize()));
        Page<PmsProduct> pmsProductPage = new Page<>(pmsProductVo.getPageNum(), pmsProductVo.getPageSize());
        log.info("pmsProductPage == {}",pmsProductPage);
        IPage<PmsProduct> page = pmsProductService.page(pmsProductPage, lqw);
        pageRequest.setList(page.getRecords());
        pageRequest.setTotal(Math.toIntExact(page.getTotal()));
        log.info("共查询到{}条记录",page.getRecords().size());
        return R.success(pageRequest);
    }

    @GetMapping("/simpleList")
    public R simpleList(@RequestParam String keyword){
        System.out.println("keyword = " + keyword);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<PmsProduct>();
        lqw.like(PmsProduct::getName,keyword)
                .or().like(PmsProduct::getProductSn,keyword);
        List<PmsProduct> list = pmsProductService.list(lqw);
        return R.success(list);
    }

    @PostMapping("/create")
    public R vreate(@RequestBody CreateVo createVo){
        System.out.println("createVo = " + createVo);
        try {
             pmsProductService.insert(createVo,0);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.success(null);
    }
    ///product/update/publishStatus
    @PostMapping("/update/publishStatus")
    public R productCategory(@RequestParam ArrayList<Integer> ids, Integer publishStatus){
        System.out.println("ids = " + ids);
        System.out.println("publishStatus = " + publishStatus);

        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setPublishStatus(publishStatus);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(PmsProduct::getId,ids);
        pmsProductService.updateByIds(pmsProduct,lqw);
        return R.success("ok");
    }
    //product/update/newStatus
    @PostMapping("/update/newStatus")
    public R updateNewStatus(@RequestParam ArrayList<Integer> ids, Integer newStatus){
        System.out.println("ids = " + ids);
        System.out.println("newStatus = " + newStatus);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setNewStatus(newStatus);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(PmsProduct::getId,ids);
        pmsProductService.updateByIds(pmsProduct,lqw);
        return R.success("ok");
    }
    //product/update/recommendStatus
    @PostMapping("/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam ArrayList<Integer> ids, Integer recommendStatus){
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setRecommandStatus(recommendStatus);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(PmsProduct::getId,ids);
        pmsProductService.updateByIds(pmsProduct,lqw);
        return R.success("ok");
    }

    @PostMapping("/update/deleteStatus")
    public R delectByIds(@RequestParam List<Long> ids,@RequestParam Integer deleteStatus){
        log.info("ids == {},deleteStatus == {}",ids,deleteStatus);
        Boolean aBoolean = pmsProductService.deleteByIds(ids,2);
        if (!aBoolean) return R.error();
        return R.success("");
    }

    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody CreateVo createVo) throws InvocationTargetException, IllegalAccessException {
        Boolean aBoolean = pmsProductService.changeById(id, createVo);
        if (!aBoolean) return R.error();
        return R.success("");
    }

    @GetMapping("/updateInfo/{id}")
    public R update(@PathVariable Long id) throws InvocationTargetException, IllegalAccessException {
        PmsProductResult pmsProductResult = pmsProductService.seleteById(id);
        return R.success(pmsProductResult);
    }

}
